<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Rzfang;
use App\common;
use App\server\RankServer;
use App\server\RedisServer;
/**
 * Undocumented class
 * 日租房
 */
class RzfangController extends Controller
{
    //列表
    public function rzfangList(){
        $data=Rzfang::paginate(5);
        return common::success(20000,$data,'cg');
    }
    //详情
    public function rzfangDetail(Request $request){
        $id=$request->get('id');
        if(empty($id)||!is_numeric($id)){
            return common::fail(2001,"",'数据错误');
        }

    //   $data=Rzfang::where('id',$id)->first()->toArray();
    //   $num=$data['look_num']+1;
    //   $data['look_num']=$num;
    //   $status= Rzfang::change('id',$id,$data);
      $status= self::update($id,'look_num');
      $data=Rzfang::where('id',$id)->first();
      if(!$status){
        return common::fail(2001,"",'网络错误');
      }

      return common::success(20000,$data,'cg');

    }
    //点赞
    public function rzfangdian(Request $request){
        $id=$request->get('id');
        if(empty($id)||!is_numeric($id)){
            return common::fail(2001,"",'数据错误');
        }

        $status= self::update($id,'d_num');
      if($status){
        return common::success(20000,"",'cg');
      }
      return common::fail(2001,"",'网络错误');
    }
    //收藏
    public function rzfangshou(Request $request){
        $id=$request->get('id');
        if(empty($id)||!is_numeric($id)){
            return common::fail(2001,"",'数据错误');
        }

        $status= self::update($id,'s_num');
        if($status){
          return common::success(20000,"",'cg');
        }
        return common::fail(2001,"",'网络错误');
    }
    //转发
    public function rzfangzhuan(Request $request){
        $id=$request->get('id');
        if(empty($id)||!is_numeric($id)){
            return common::fail(2001,"",'数据错误');
        }

        $status= self::update($id,'z_num');
        if($status){
          return common::success(20000,"",'cg');
        }
        return common::fail(2001,"",'网络错误');
    }
    //修改数量
    public static function update($id,$where){
        $data=Rzfang::where('id',$id)->first();
        $num=$data[$where]+1;
        $status=Rzfang::where('id',$id)->update([$where=>$num]);
        RankServer::rzfrank($data['name']);
        return $status;
    }
     //排行
     public function rzf_day_rank(){
        $redis=RedisServer::createRedisObject();
        $redis->select(8);
        //日榜
        $key='rzf_dayrank';
        return $redis->zRevRange($key,0,9);
    }
     //周榜
     public function rzf_week_rank(){
        $redis=RedisServer::createRedisObject();
        $redis->select(8);
        $key='rzf_week_rank';
        return $redis->zRevRange($key,0,9);
    }
    //redis日租房
    public function rzf_redis(){
        $rzfdata=Rzfang::get()->toArray();
        $redis=RedisServer::createRedisObject();
        $redis->select(8);
        $key='rzf_list';
        $data=json_encode($rzfdata);
        return $redis->Data_lpush($key,$data);
    }





}
